<template>
  <div class="radioCategory w">
    <div class="main">
      <div
        class="main_item"
        v-for="item in categories"
        :key="item.id"
        @click="toRadioCategoryPage(item.id)"
      >
        <span
          :style="{
            backgroundImage: 'url(' + item.picWebUrl + ')',
          }"
          :class="$route.params.id == item.id ? 'active' : null"
        >
        </span>
        <p>{{ item.name }}</p>
      </div>
    </div>
    <h2>优秀新电台</h2>
    <div class="new">
      <div class="new_item" v-for="item in recommend" :key="item.id">
        <GlobalImageDeep :url="item.picUrl" :imgWidth="224" :imgHeight="224" />
        <div class="name one_txt_cut" @click="toRadioDetailsPage(item.id)">
          {{ item.name }}
        </div>
        <p class="author">{{ item.rcmdtext }}</p>
      </div>
    </div>
    <h2>电台排行榜</h2>
    <div class="new hotArdio">
      <div class="new_item" v-for="item in hotRadio" :key="item.id">
        <GlobalImageDeep :url="item.picUrl" :imgWidth="224" :imgHeight="224" />
        <div class="name one_txt_cut" @click="toRadioDetailsPage(item.id)">
          {{ item.name }}
        </div>
        <p class="author">{{ item.rcmdtext }}</p>
      </div>
    </div>
    <!-- 分页 -->
    <el-row type="flex" class="row-bg" justify="center">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="queryInfo.limit"
        @current-change="paginationCurrentChange"
      >
      </el-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  getRadioCategory,
  getCategoryRecommend,
  getHotRadio,
} from "../../../../api/radioApi";
export default {
  name: "radioCategory",
  components: {},
  data() {
    return {
      categories: [], // 电台分类的数据
      queryInfo: {
        cateId: this.$route.params.id,
        offset: 0,
        limit: 30,
      },
      total: 0,
      currentPage: 1,
      hotRadio: [],
      recommend: [],
    };
  },
  created() {
    this.getRadioCategory();
    this.getCategoryRecommend();
    this.getHotRadio();
  },
  watch: {
    "$route.params.id": {
      handler(newValue) {
        console.log("发生了变化");
        this.getCategoryRecommend();
        this.queryInfo = {
          cateId: newValue,
          offset: 0,
          limit: 30,
        };
        this.total = 0;
        this.currentPage = 1;
        this.getHotRadio();
      },
    },
  },
  methods: {
    // 获取所有的分类
    async getRadioCategory() {
      try {
        let result = await getRadioCategory();
        if (result.code === 200) {
          this.categories = result.categories;
        }
      } catch (error) {}
    },
    // 获取 热门电台
    async getHotRadio() {
      let result = await getHotRadio(this.queryInfo);
      if (result.code === 200) {
        this.hotRadio = result.djRadios;
        this.total = result.count;
      }
    },
    async getCategoryRecommend() {
      let result = await getCategoryRecommend(this.$route.params.id);
      if (result.code === 200) {
        this.recommend = result.djRadios;
      }
    },
    toRadioCategoryPage(id) {
      this.$router.push(`/radio/category/${id}`);
    },
    paginationCurrentChange(newPage) {
      this.currentPage = newPage;
      this.queryInfo.offset = (newPage - 1) * this.queryInfo.limit;
      this.getHotRadio();
      this.$utils.scrollTop(
        document.querySelector(".hotArdio").offsetTop - 80,
        200
      );
    },
    toRadioDetailsPage(id) {
      this.$router.push(`/radioDetails/${id}`);
    },
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.radioCategory {
  /deep/ .el-pager li.active {
    color: #fff;
    background: @globalColor;
    border-radius: 50%;
    &:hover {
      color: #fff;
    }
  }
  /deep/ .el-pager li:hover {
    color: @globalColor;
  }
  padding: 30px 0;

  > h2 {
    font-weight: normal;
    padding-bottom: 10px;
    border-bottom: 1px solid @globalColor;
    margin-bottom: 20px;
    font-size: 24px;
  }
  .main {
    // background-color: #f5f5f5;
    display: flex;
    flex-wrap: wrap;
    .main_item {
      display: flex;
      width: 200px;
      height: 80px;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: 20px;
      cursor: pointer;
      &:hover {
        background-color: #f6f7f7;
      }
      span {
        width: 48px;
        height: 48px;
        background-repeat: no-repeat;
        background-position: 0 0;
        &.active {
          background-position: -48px 0;
        }
      }
      p {
        &:hover {
          color: @globalColor;
        }
      }
    }
  }
  .new {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    .new_item {
      width: 224px;
      margin-bottom: 20px;
      /deep/ .el-image img {
        border-radius: 15px;
      }
      .name {
        margin-top: 10px;
        font-size: 14px;
        cursor: pointer;
        &:hover {
          color: @globalColor;
        }
      }
      .author {
        font-size: 14px;
        margin-top: 10px;
        color: #999999;
      }
    }
  }
}
</style>
